
package com.kaluli.project.controller;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJacksonJsonView;

import com.alibaba.fastjson.JSON;
import com.kaluli.project.domain.entity.KllHupuApiMVO;
import com.kaluli.project.domain.entity.KllHupuApiWVO;
import com.kaluli.project.domain.repository.KllHupuApiMVORepository;
import com.kaluli.project.domain.repository.KllHupuApiWVORepository;
import com.kaluli.project.service.KllMainOrderService;
import com.kaluli.project.utils.TimeUtil;

/**
 * 
 * 商品信息
 * 
 * @author niwei
 * 
 */
@Controller
@PropertySource({ "classpath:thridsystem.properties" })
public class HupuApiController {

	private static Logger log = Logger.getLogger(HupuApiController.class);

	@Autowired
	Environment env;


	@Autowired
	private KllHupuApiMVORepository kllHupuApiMVORepository;
	
	@Autowired
	private KllHupuApiWVORepository kllHupuApiWVORepository;
	
	@RequestMapping(value = "/m/getHomeJson", method = RequestMethod.POST)
	@ResponseBody
	public ModelAndView getJson(@RequestBody byte[] bytes) throws UnsupportedEncodingException {
		Map<String, Object> map = new HashMap<>();
		List<KllHupuApiMVO> list = new ArrayList<>();
		try {
			 list= kllHupuApiMVORepository.findListByjson();
			 map.put("status", 200);
			 map.put("msg", "ok");
			
		} catch (Exception e) {
			map.put("status", -1);
			map.put("msg", e.getMessage());
			log.info("/m/getHomeJson_error="+e.getMessage());
		}
		map.put("data", list);
		String response = JSON.toJSONString(map);
		log.info("/m/getHomeJson_data="+response);
		return new ModelAndView(new MappingJacksonJsonView(), map);

	}
	
	@RequestMapping(value = "/m/getNewsJson", method = RequestMethod.POST)
	@ResponseBody
	public ModelAndView getViewJson(@RequestBody byte[] bytes) throws UnsupportedEncodingException {
		Map<String, Object> map = new HashMap<>();
		List<KllHupuApiWVO> list = new ArrayList<>();
		try {
			 list= kllHupuApiWVORepository.findListByjson();
			 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			 for(KllHupuApiWVO vo:list){
				 vo.setTime(TimeUtil.showTime(df.parse(vo.getTime())));
			 }
			 map.put("status", 200);
			 map.put("msg", "ok");
		} catch (Exception e) {
			map.put("status", -1);
			map.put("msg", e.getMessage());
			log.info("/m/getNewsJson_error="+e.getMessage());
		}
		map.put("data", list);
		String response = JSON.toJSONString(map);
		log.info("/m/getNewsJson_data="+response);
		return new ModelAndView(new MappingJacksonJsonView(), map);

	}
	
	
	@RequestMapping(value = "/pc/getFlowtitleJson", method = RequestMethod.POST)
	@ResponseBody
	public ModelAndView getFlowtitleJson(@RequestBody byte[] bytes) throws UnsupportedEncodingException {
		Map<String, Object> map = new HashMap<>();
		List<KllHupuApiMVO> list = new ArrayList<>();
		try {
			 list= kllHupuApiMVORepository.findListByTime();
			 map.put("status", 200);
			 map.put("msg", "ok");
			
		} catch (Exception e) {
			map.put("status", -1);
			map.put("msg", e.getMessage());
			log.info("/m/getHomeJson_error="+e.getMessage());
		}
		map.put("data", list);
		String response = JSON.toJSONString(map);
		log.info("/m/getHomeJson_data="+response);
		return new ModelAndView(new MappingJacksonJsonView(), map);

	}
	
}
